package com.qlk.vorder.rest;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.qlk.common.utils.wrap.WrapMapper;
import com.qlk.common.utils.wrap.Wrapper;
import com.qlk.vorder.domain.ReserveSession;
import com.qlk.vorder.sdk.api.request.ReserveSessionRequest;
import com.qlk.vorder.sdk.api.response.ReserveSessionResponse;
import com.qlk.vorder.service.ReserveSessionService;

/**
 * 预约订单会话记录REST服务：提供有关预约订单会话记录的接口
 * 
 * @author zhaoyanan
 * @version 2016-7-28 14:14:01
 * 
 */
@Component
@Path("services")
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
public class ReserveSessionResource {

	private final Log logger = LogFactory.getLog(this.getClass());

	@Autowired
	private ReserveSessionService reserveSessionService;

	/**
	 * 查询预约订单会话记录信息
	 * 
	 * @param request
	 *            预约订单会话记录请求参数
	 * @return 预约订单会话记录返回对象
	 * 
	 */
	@POST
	@Path("/reserveSession/getReserveSession")
	public Wrapper<?> getReserveSession(ReserveSessionRequest request) {
		if (null == request || null == request.getId()) {
			this.logger.error("getReserveSession 传入参数有误");
			return WrapMapper.illegalArgument();
		}

		try {
			ReserveSession reserveSession = reserveSessionService.getReserveSessionById(request.getId());
			ReserveSessionResponse response = convert(reserveSession);
			return WrapMapper.ok().result(response);
		} catch (Exception e) {
			this.logger.error("查询预约订单会话记录数据异常", e);
			return WrapMapper.error();
		}
	}

	// 数据转换
	private ReserveSessionResponse convert(ReserveSession reserveSession) {
		if (null == reserveSession) {
			return null;
		}

		ReserveSessionResponse reserveSessionResponse = new ReserveSessionResponse();
		BeanUtils.copyProperties(reserveSession, reserveSessionResponse);
		return reserveSessionResponse;
	}

	// 数据转换
	private List<ReserveSessionResponse> convertList(List<ReserveSession> reserveSessions) {
		if (CollectionUtils.isEmpty(reserveSessions)) {
			return null;
		}

		List<ReserveSessionResponse> list = new ArrayList<ReserveSessionResponse>(reserveSessions.size());
		for (ReserveSession reserveSession : reserveSessions) {
			list.add(convert(reserveSession));
		}
		return list;
	}

}
